// pages/my/my-wallet/my-addMoney/my-addMoney.js
var request = require('../../../../utils/request.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      curMoney: options.money,
      order_no: options.order_no
    })
    that.getToken();
  },

  getToken: function () {
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })

      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },

  onShow: function () {
  
  },
  //去支付
  goPay:function(){
    var that = this;
    var postData = {};
    postData.session_id = that.data.token;
    postData.order_no = that.data.order_no;
    console.log(postData)
    request.rechargePay(
      postData,
      (res) => {
        console.log(res);
        if (res.data.status == "error") {
          wx.showToast({
            title: res.data.msg,
            image:"../../../images/xx.png",
            success:setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
            },800)
          })
        } else {
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': function (res) {
              console.log(res);
              wx.showModal({
                title: '',
                content: '支付成功',
                showCancel: false,
                confirmText: "返回",
                confirmColor: "#FF0000",
                success: function (res) {
                  wx.navigateBack({
                    delta:1
                  })
                }
              })
            },
            'fail': function (res) {
              console.log(res)
              wx.showModal({
                title: '支付失败',
                content: '请重新支付',
                showCancel: false,
                confirmText: "我知道了",
                confirmColor: "#FF0000",
              })
            }
          })
        }

      },
    )
  }
  
})